package com.sundi.ecgeneralize.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecgeneralize.dto.CouponDto;
import com.sundi.ecgeneralize.dto.ResultEntity;
import com.sundi.ecgeneralize.dto.TopicDto;
import com.sundi.ecgeneralize.entity.EcCoupon;
import com.sundi.ecgeneralize.entity.EcTopic;
import com.sundi.ecgeneralize.service.IEcTopicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 专题表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-26
 */
@RestController
@RequestMapping("/ecTopic")
@CrossOrigin(value = "*",  allowCredentials = "true")
public class EcTopicController {
    @Autowired
    IEcTopicService ecTopicService;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    //    查询
    @RequestMapping(value = "/topicList", method = RequestMethod.GET)
    public ResultEntity getTopicList(TopicDto topicDto) {
        PageHelper.startPage(topicDto.getPageNum(), topicDto.getPageSize());
        List<EcTopic> ecTopics = ecTopicService.selectTopicList(topicDto);
        PageInfo<EcTopic> info = new PageInfo<>(ecTopics);
//        Wrapper<EcCoupon> wrapper = new EntityWrapper<EcCoupon>();
//        List<EcCoupon> ecCoupons = ecCouponService.selectList(wrapper);
//        logger.info(JSON.toJSONString(ecCoupons));
        return ResultEntity.ok(info);
    }


    //    添加
    @RequestMapping(value = "/addTopic")
    public ResultEntity addCoupon(@RequestBody TopicDto topicDto) {
        System.out.println(topicDto);
        EcTopic ecTopic = new EcTopic();
        BeanUtils.copyProperties(topicDto, ecTopic);
        if (ecTopic.getId() == null) {
            ecTopic.setAddTime(new Date());
        }
        boolean insert = ecTopicService.insert(ecTopic);
        return ResultEntity.ok(insert);
    }

    //    删除
    @RequestMapping(value = "/delTopic")
    public ResultEntity del(Integer id) {
        System.err.println(id);
        ecTopicService.updAndDel(id);
        return ResultEntity.ok();
    }

    //回显
    @RequestMapping(value = "/getTopic")
    public ResultEntity getTopic(Integer id) {
        EcTopic topic = ecTopicService.getTopic(id);
        System.err.println(topic);
        return ResultEntity.ok(topic);
    }
    //    修改
    @RequestMapping(value = "/updTopic")
    public ResultEntity updTopic(@RequestBody TopicDto topicDto) {
        System.out.println(topicDto);
        EcTopic ecTopic = new EcTopic();
        BeanUtils.copyProperties(topicDto, ecTopic);
        if (ecTopic.getId() != null) {
            ecTopic.setUpdateTime(new Date());
        }
        boolean b = ecTopicService.updateById(ecTopic);
        return ResultEntity.ok(b);
    }

    //    批量删除
    @RequestMapping(value = "/delAll")
    public ResultEntity delAll(String ids) {
        System.err.println(ids);
        ecTopicService.deleteAll(ids);
        return ResultEntity.ok();
    }

    @RequestMapping(value = "/getPic")
    public ResultEntity getPic(Integer id){
        EcTopic pic = ecTopicService.getPic(id);
        System.err.println(pic);
        return ResultEntity.ok(pic);
    }

}
